<!doctype html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
  <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
  <title>登录</title>
  <link rel="stylesheet" type="text/css" href="../css/api.css" />
  <link rel="stylesheet" type="text/css" href="../css/login.css" />
</head>

<body>
  <div id="app" v-cloak>
    <div class="wrap flex-wrap flex-vertical">
      <header id="header">
        <div class="header-wrap">
          <div @click="goback" class="header-left">
            <i class="iconfont icon-pre"></i>
          </div>
          <div class="header-title">登录</div>
        </div>
      </header>
      <div id="main" class="flex-con">
        <div class="form-wrap">
          <div class="form-item flex-wrap">
            <i class="iconfont icon-shouji"></i>
            <input v-model="mobile" class="flex-con" type="tel"  maxlength="11" placeholder="请输入手机号码">
          </div>
          <div class="form-item flex-wrap">
            <i class="iconfont icon-mima"></i>
            <input v-model="password" class="flex-con" type="password" placeholder="请输入密码">
          </div>
          <div class="btn-wrap">
            <button @click="login" id="submit-btn" type="button" :disabled="isDisabled" :class="[!isDisabled ? 'btn-active' : 'btn']">立即登录</button>
          </div>
        </div>
        <div class="forget-wrap flex-wrap">
          <a @click="goRegister" class="register-link" href="javascript:;">还没有账号？立即注册</a>
          <a @click="goforget" class="forget-pwd" href="javascript:;">忘记密码?</a>
        </div>
        <div class="agreement-wrap">
          <span>注册或登录即代表你同意</span>
          <strong @click="openProtocol">《用户协议》</strong>
        </div>
      </div>
    </div>
  </div>
</body>

</html>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/vue.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript">
  var app, rong;
  apiready = function () {
    $api.fixStatusBar($api.dom('#header'));
    api.setStatusBarStyle({
      style: 'light',
    });
    app = new Vue({
      el: '#app',
      data: {
        mobile: '',
        password: '',
        isDisabled: true,
      },
      created: function () {
        var _this = this;
        if (!!$api.getStorage('uid')) {
          api.openWin({
            name: 'main',
            url: 'main.html',
            slidBackEnabled: false
          });
        }
        if (!$api.getStorage('deviceToken')) {
          _this.regDevice();
        }
      },
      mounted: function () {},
      methods: {
        watchModel: function () {
          if (this.mobile == '' || this.password == '') {
            this.isDisabled = true;
          } else {
            this.isDisabled = false;
          }
        },
        
        goback: function () {
          api.closeWin();
        },

        goRegister: function () {
          api.openWin({
            name: 'register',
            url: 'register.html',
            slidBackEnabled: false
          });
        },

        goforget: function () {
          api.openWin({
            name: 'forget',
            url: 'forget.html',
            slidBackEnabled: false
          });
        },

        login: function () {
          var _this = this;
          var regex = /^(?=\d{11}$)^1(?:3\d|4[57]|5[^4\D]|66|7[^249\D]|8\d|9[89])\d{8}$/g;
          if (!regex.test(_this.mobile)) {
            api.toast({
              msg: '请输入正确的手机号码',
              duration: 2000,
              location: 'bottom'
            });
            return false;
          }
          if (_this.password == '') {
            api.toast({
              msg: '密码不能为空',
              duration: 2000,
              location: 'bottom'
            });
            return false;
          }
          api.showProgress({
            title: '',
            text: '正在登录...'
          });
          api.ajax({
            url: baseUrl + 'oauth/login',
            method: 'post',
            headers: {
              token: $api.getStorage('deviceToken')
            },
            data: {
              values: {
                mobile: _this.mobile,
                password: _this.password,
              },
            }
          }, function (ret, err) {
            api.hideProgress();
            if (ret) {
              api.toast({
                msg: ret.message[0]
              });
              if (ret.status == '200') {
                var token = JSON.parse(ret.data.token).code == 200 ? JSON.parse(ret.data.token).token : '';
                $api.setStorage('uid', ret.data.id);
                $api.setStorage('uname', ret.data.name);
                $api.setStorage('uavatar', ret.data.avatar);
                $api.setStorage('umobile', ret.data.email);
                $api.setStorage('token', token);
                api.openWin({
                  name: 'main',
                  url: 'main.html',
                  slidBackEnabled: false
                });
              }
            }
            if (err) {
              api.toast({
                msg: '网络错误，请稍后再试'
              });
            }
          });
        },

        openProtocol: function () {
          api.openWin({
            name: 'protocol',
            url: 'protocol.html',
            bounces: false,
            pageParam: {
              key: 'value'
            }
          });
        },

        // 设备注册
				regDevice: function () {
					api.ajax({
						url: baseUrl + 'device/add',
						method: 'post',
						data: {
							values: {
								model: api.deviceModel,
								device_code: api.deviceId,
								pixelRatio: '2',
								windowWidth: api.screenWidth + '',
								windowHeight: api.screenHeight + '',
								language: api.systemType,
								appVersion: api.appVersion,
								version: api.version,
								channel: $api.getStorage('channel'),
								tuid: $api.getStorage('tuid'),
							},
						}
					}, function (ret, err) {
						if (ret && ret.status == 200) {
							$api.setStorage('deviceToken', ret.data.token);
						}
						if (err) {
							api.toast({
								msg: '网络错误，请稍后再试'
							});
						}
					});
				},
      },
      watch: {
        'mobile': function () {
          this.watchModel()
        },
        'password': function () {
          this.watchModel()
        },
      }
    })
  }
</script>